<?php 
	//中文编码
	header("Content-type:text/html;charset=utf-8");
	include_once("function.php") ;
	/**
	*   基础表 用于存储方法 
	*	链接数据库方法
	*	添加财务日志方法
	*	获取列表方法
	*/
	class base{
		//设置链接数据库参数
		private $db;
		private $host;
		private $user;
		private $pwd;
		private $name;
		private $charset;
		protected $config;
		protected $set;
		function __construct(){
			//获取数据库链接
			//$this->db = include_once('../App/Common/Conf/db.php');
			//$this->db = include_once('../App/Common/Conf/db.php');
			$this->host    = '127.0.0.1';
			$this->user    = 'root';
			$this->pwd     = 'root';
			$this->name    = 'lz';
			$this->charset = 'utf8';
			$this->set = $_opts_values = array(PDO::ATTR_PERSISTENT=>true,PDO::ATTR_ERRMODE=>2,PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES utf8');
			//获取配置项
			//$this->config=$this->config();
			//不执行自动程序
			// if($this->config['auto_switch']!=1){
				// return;
			// }
			
		}
		/***************************数据库链接部分*******************************************/
		//执行语句
		protected function query($sql){
			$dsn = "mysql:host=".$this->host.";dbname=".$this->name;
			$db = new PDO($dsn, $this->user, $this->pwd);
// 			dump($sql);
			$stmt=$db->query($sql);
			if($stmt){
				$rows=$stmt->fetchall(PDO::FETCH_ASSOC);
				return $rows;
			}
			return false;
		}
		protected function select($sql){
			//创建PDO对象
			$dsn = "mysql:host=".$this->host.";dbname=".$this->name;
			$db = new PDO($dsn, $this->user, $this->pwd,$this->set);
			$stmt=$db->query($sql);
			if($stmt){
				$rows=$stmt->fetchall(PDO::FETCH_ASSOC);
				return $rows;
			}
			return false;
		}
		/**
		 * 批量修改数据
		 */
// 			$data = [
		// 				[
		// 					'id'=>1,
		// 					'doge'=>'doge+1200',
		// 				],
		// 				[
		// 					'id'=>2,
		// 					'doge'=>'doge+1000',
		// 				]
		// 			];
		// 			$r = $this->update('yang_wallet', 'id', 'doge', $data);
		// 			var_dump($r);die;
		public function update($tableName,$key,$value,$data){
			//创建PDO对象
			$dsn = "mysql:host=".$this->host.";dbname=".$this->name;
			$db = new PDO($dsn, $this->user, $this->pwd);
			$update_sql = 'UPDATE '.$tableName.' SET '.$value.' = ( CASE '.$key." ";
			foreach($data as $k => $item) {
				$update_sql .= "WHEN ".$item[$key] . " THEN ".$item[$value]. " ";
			}
			$update_sql .= "ELSE ".$value;
			$update_sql .= " END )";
			$r = $db->exec($update_sql);
			if($r){
				return true;
			}
			return false;
		}
		
		/**
		 * 查询单一数据
		 * @param unknown $sql
		 * @return unknown|boolean
		 */
		protected function find($sql){
			//创建PDO对象
			$dsn = "mysql:host=".$this->host.";dbname=".$this->name;
			$db = new PDO($dsn, $this->user, $this->pwd,$this->set);
			$stmt=$db->query($sql);
			if($stmt){
				$rows=$stmt->fetchall(PDO::FETCH_ASSOC);
				$rows = $rows[0];
				return $rows;
			}
			return false;
		}

		protected  function insert($table,$arrData) {
			$dsn = "mysql:host=".$this->host.";dbname=".$this->name;
			$db = new PDO($dsn, $this->user, $this->pwd,$this->set);
			$name = $values = '';
			$flag = $flagV = 1;
			$true = is_array( current($arrData) );//判断是否一次插入多条数据
			if($true) {
				//构建插入多条数据的sql语句
				foreach($arrData as $arr) {
					$values .= $flag ? '(' : ',(';
					foreach($arr as $key => $value) {
						if($flagV) {
							if($flag) $name .= "$key";
							$values .= "'$value'";
							$flagV = 0;
						} else {
							if($flag) $name .= ",$key";
							$values .= ",'$value'";
						}
					}
					$values .= ') ';
					$flag = 0;
					$flagV = 1;
				}
			} else {
				//构建插入单条数据的sql语句
				foreach($arrData as $key => $value) {
					if($flagV) {
						$name = "$key";
						$values = "('$value'";
						$flagV = 0;
					} else {
						$name .= ",$key";
						$values .= ",'$value'";
					}
				}
				$values .= ") ";
			}
		
			$sql = $strSql = "insert into $table ($name) values $values";
			if( ($this->result = $db->exec($strSql) ) > 0 ) {
				return true;
			}
			return false;
		}

		/**
		 * 新增一条数据
		 */
		public function addOne($table,$data){
			$dsn = "mysql:host=".$this->host.";dbname=".$this->name;
			$db = new PDO($dsn, $this->user, $this->pwd,$this->set);
			$name = $values = '';
			$flag = $flagV = 1;
			foreach($data as $key => $value) {
				if($flagV) {
					$name = "$key";
					$values = "('$value'";
					$flagV = 0;
				} else {
					$name .= ",$key";
					$values .= ",'$value'";
				}
			}
			$values .= ") ";
			$sql = $strSql = "insert into $table ($name) values $values";
			if( ($this->result = $db->exec($strSql) ) > 0 ) {
				return $db->lastInsertId();
			}
		}
	}
?>